package com.wap.framework.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.oreilly.servlet.MultipartRequest;
import com.wap.framework.dao.ImageDAO;
import com.wap.framework.model.Image;

public class AdminUpload extends HttpServlet {

	private static final long serialVersionUID = -3140554472146162982L;

	private static String uploadPath = Constants.UPLOAD_IMAGES_PATH;
	private static int maxPostSize = 5 * 1024 * 1024; // 5MB
	private static String encoding = "UTF-8";
	
	public AdminUpload() {
		super();
	}
	
	public void destroy() {
		super.destroy();
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
//		String dir = AdminUpload.class.getClassLoader().getResource("").toString();
//		uploadPath = dir.replaceAll("file:/(.*)/WEB-INF/classes/", "$1/upload_images");
		try {
			File filePath = new File(uploadPath);
			if(!filePath.exists()) {
				filePath.mkdirs();
			}
			
			MultipartRequest multiRequest = new MultipartRequest(request,
					uploadPath, maxPostSize, encoding);		
			
			List<Image> images = new ArrayList<Image>();
			Enumeration<String> filesName = multiRequest.getFileNames();			
			while(filesName.hasMoreElements()) {
				String name = filesName.nextElement();				
				String fileName = multiRequest.getFilesystemName(name);
				String remark = multiRequest.getParameter(name);				
				if(fileName != null) {
					fileName = java.net.URLEncoder.encode(fileName);
					Image image = new Image();
					image.setImageName(fileName);
					image.setDisplayName(fileName);
					image.setImagePath(uploadPath);
					image.setRemark(remark == null ? "" : remark);
					images.add(image);
				}
			}		
						
			if(images.size() > 0) {
				ServletContext context = this.getServletContext();
				WebApplicationContext webAppCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
				ImageDAO imageDAO = (ImageDAO)webAppCtx.getBean("imageDAO");
				
				Image[] imageArray = new Image[images.size()];
				for(int i = 0; i < images.size(); i++) {
					imageArray[i] = images.get(i);
				}
				imageDAO.saveImages(imageArray);
			}						
			
			response.sendRedirect(request.getContextPath() + "/imageMgr.do?action_method=list");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}	
	
	public void init() throws ServletException {		
		super.init();		
	}
}
